﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace my_proxy.RequestHandlers
{
    class Tripledes
    {
        CipherMode mode = CipherMode.CBC;
        PaddingMode padMod = PaddingMode.PKCS7;
        byte[] bytes = new byte[8];
        string key = "abcdefghijklmnop";


        public string Encrypt(string toEncrypt)
        {
            TripleDES enc =new TripleDESCryptoServiceProvider();
            enc.IV = bytes;
            enc.Mode = mode;
            enc.Padding = padMod;
            enc.Key = System.Text.Encoding.UTF8.GetBytes(key);
            ICryptoTransform cry = enc.CreateEncryptor();
            byte[] encriptString = UTF8Encoding.UTF8.GetBytes(toEncrypt);
            byte[] array = cry.TransformFinalBlock(encriptString, 0, encriptString.Length);
            return Convert.ToBase64String(array, 0, array.Length);
        }

        public string decryption(string tobDecry)
        {
            TripleDES enc = new TripleDESCryptoServiceProvider();
            enc.IV = bytes;
            enc.Mode = mode;
            enc.Padding = padMod;
            enc.Key = System.Text.Encoding.UTF8.GetBytes(key);
            ICryptoTransform cry = enc.CreateDecryptor();
            byte[] encriptString = Convert.FromBase64String(tobDecry);
            byte[] array = cry.TransformFinalBlock(encriptString, 0, encriptString.Length);
            return UTF8Encoding.UTF8.GetString(array, 0, array.Length);
        }

        public string CutPrefixSufix(string toDo)
        {
            //prefix
            if(toDo.ToLower().StartsWith("http://"))
            {
                toDo = toDo.Substring("http://".Length);
            }
            else if (toDo.StartsWith("https://"))
            {
                toDo = toDo.Substring("https://".Length);
            }

            if (toDo.ToLower().StartsWith("www"))
            {
                toDo = toDo.Substring("www.".Length);
            }
            // check "/"
            if (toDo.Contains("/"))
            {
                int index = toDo.IndexOf('/');
                toDo = toDo.Substring(0, index);
            }

            return toDo;
        }
     

    }
 }